package com.birbit.android.jobqueue.examples.twitter.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.birbit.android.jobqueue.examples.twitter.entities.Tweet;
import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.AbstractDaoSession;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.internal.DaoConfig;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/**
 * DAO for table TWEET.
*/
public class TweetDao extends AbstractDao<Tweet, Long> {

    public static final String TABLENAME = "TWEET";

    /**
     * Properties of entity Tweet.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property
            LocalId =new Property(0, Long.class , "localId", true, "LOCAL_ID");
        public final static Property ServerId =new Property(1, Long.class , "serverId", false, "SERVER_ID");
        public final static Property Text =new Property(2, String.class , "text", false, "TEXT");
        public final static Property UserId =new Property(3, Long.class , "userId", false, "USER_ID");
        public final static Property IsLocal =new Property(4, Boolean.class , "isLocal", false, "IS_LOCAL");
        public final static Property CreatedAt =new Property(5, java.util.Date.class , "createdAt", false, "CREATED_AT");
    }


    public TweetDao(DaoConfig config) {
        super(config);
    }

    public TweetDao(DaoConfig config, AbstractDaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'TWEET' (" + //
                "'LOCAL_ID' INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: localId
                "'SERVER_ID' INTEGER UNIQUE ," + // 1: serverId
                "'TEXT' TEXT," + // 2: text
                "'USER_ID' INTEGER," + // 3: userId
                "'IS_LOCAL' INTEGER," + // 4: isLocal
                "'CREATED_AT' INTEGER);"); // 5: createdAt
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'TWEET'";
        db.execSQL(sql);
    }

    @Override
    protected void bindValues(DatabaseStatement stmt, Tweet entity) {
        stmt.clearBindings();
        entity.onBeforeSave();
 
        Long localId = entity.getLocalId();
        if (localId != null) {
            stmt.bindLong(1, localId);

        }
 
        Long serverId = entity.getServerId();
        if (serverId != null) {
            stmt.bindLong(2, serverId);

        }
 
        String text = entity.getText();
        if (text != null) {
            stmt.bindString(3, text);

        }
 
        Long userId = entity.getUserId();
        if (userId != null) {
            stmt.bindLong(4, userId);

        }
 
        Boolean isLocal = entity.getIsLocal();
        if (isLocal != null) {
            stmt.bindLong(5, isLocal ? 1l: 0l);

        }
 
        java.util.Date createdAt = entity.getCreatedAt();
        if (createdAt != null) {
            stmt.bindLong(6, createdAt.getTime());

        }
    }

    @Override protected void bindValues(SQLiteStatement stmt, Tweet entity) {
        stmt.clearBindings();
        entity.onBeforeSave();

        Long localId = entity.getLocalId();
        if (localId != null) {
            stmt.bindLong(1, localId);

        }

        Long serverId = entity.getServerId();
        if (serverId != null) {
            stmt.bindLong(2, serverId);

        }

        String text = entity.getText();
        if (text != null) {
            stmt.bindString(3, text);

        }

        Long userId = entity.getUserId();
        if (userId != null) {
            stmt.bindLong(4, userId);

        }

        Boolean isLocal = entity.getIsLocal();
        if (isLocal != null) {
            stmt.bindLong(5, isLocal ? 1l: 0l);

        }

        java.util.Date createdAt = entity.getCreatedAt();
        if (createdAt != null) {
            stmt.bindLong(6, createdAt.getTime());

        }
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }

    /** @inheritdoc */
    @Override
    public Tweet readEntity(Cursor cursor, int offset) {
        Tweet entity = new Tweet( //

            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0) , // localId
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1) , // serverId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2) , // text
            cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3) , // userId
            cursor.isNull(offset + 4) ? null : cursor.getShort(offset + 4) != 0 , // isLocal
            cursor.isNull(offset + 5) ? null : new java.util.Date( cursor.getLong(offset + 5) ) // createdAt
        );
        return entity;
    }

    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, Tweet entity, int offset) {
        entity.setLocalId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0) );
        entity.setServerId(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1) );
        entity.setText(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2) );
        entity.setUserId(cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3) );
        entity.setIsLocal(cursor.isNull(offset + 4) ? null : cursor.getShort(offset + 4) != 0 );
        entity.setCreatedAt(cursor.isNull(offset + 5) ? null : new java.util.Date( cursor.getLong(offset + 5) ) );
     }

    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(Tweet entity, long rowId) {
        entity.setLocalId(rowId);
        return rowId;
    }

    /** @inheritdoc */
    @Override
    public Long getKey(Tweet entity) {
        if(entity != null) {
            return entity.getLocalId();
        } else {
            return null;
        }
    }

    @Override protected boolean hasKey(Tweet entity) {
        return true;
    }

    /** @inheritdoc */
    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }

}
